package com.sskj.phoenixcoin.activity;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sskj.common.util.CheckUtil;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.util.HideUtil;
import com.sskj.phoenixcoin.R;
import com.sskj.phoenixcoin.component.DaggerUserDataComponent;
import com.sskj.phoenixcoin.presenter.AppSetTpwdActivityPresenter;

import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subscribers.DisposableSubscriber;


public class AppSetTpwdActivity extends BaseActivity<AppSetTpwdActivityPresenter> {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.right_tv)
    TextView rightTv;
    @BindView(R.id.tv_tel)
    TextView tvTel;
    @BindView(R.id.first_tv_show)
    TextView firstTvShow;
    @BindView(R.id.money_edit)
    EditText moneyEdit;
    @BindView(R.id.first_line_show)
    View firstLineShow;
    @BindView(R.id.second_tv_show)
    TextView secondTvShow;
    @BindView(R.id.num_edit)
    EditText numEdit;
    @BindView(R.id.second_line_show)
    View secondLineShow;
    @BindView(R.id.withdraw_edit)
    EditText withdrawEdit;
    @BindView(R.id.get_code)
    TextView getCode;
    @BindView(R.id.three_ll)
    LinearLayout threeLl;
    @BindView(R.id.app_exit_login)
    Button appExitLogin;
    @Inject
    UserViewModel userViewModel;
    private UserData userData;
    private DisposableSubscriber<Long> disposableSubscriber;


    @Override
    protected int getLayoutResId() {
        return R.layout.app_activity_set_tpwd;
    }

    @Override
    public AppSetTpwdActivityPresenter getPresenter() {
        return new AppSetTpwdActivityPresenter();
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, AppSetTpwdActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        super.initView();
        DaggerUserDataComponent.create().inject(this);
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
                tvTel.setText("手机号: " + HideUtil.getPhoneHide(userData.getTel()));
                tvTitle.setText(userData.getPhoneixPwd().equals("1") ? "修改交易密码" : "设置交易密码");
            } else {
                userData = null;
            }
        });
        ClickUtil.click(ivBack, () -> {
            finish();
        });
        ClickUtil.click(getCode, () -> {
            if (TextUtils.isEmpty(userData.getTel())) {
                ToastUtil.showShort("当前手机号为空");
                return;
            }
            mPresenter.getCode(userData.getTel(), "4");
        });
        ClickUtil.click(appExitLogin, () -> {
            String tpwd = moneyEdit.getText().toString();
            String tpwdSecond = numEdit.getText().toString();
            String code = withdrawEdit.getText().toString().trim();
            if (TextUtils.isEmpty(tpwd)) {
                ToastUtil.showShort("请输入交易密码");
                return;
            }
            if (!CheckUtil.isPayPwd(tpwd)) {
                ToastUtil.showShort("您的交易密码格式错误");
                return;
            }
            if (TextUtils.isEmpty(tpwdSecond)) {
                ToastUtil.showShort("请再次输入交易密码");
                return;
            }
            if (!CheckUtil.isPayPwd(tpwdSecond)) {
                ToastUtil.showShort("确认交易密码格式错误");
                return;
            }
            if (!tpwd.equals(tpwdSecond)) {
                ToastUtil.showShort("两次交易密码输入不一致");
                return;
            }
            if (TextUtils.isEmpty(code)) {
                ToastUtil.showShort("请输入验证码");
                return;
            }
            mPresenter.updateTpwd(String.valueOf(userData.getId()), tpwd, userData.getTel(), code);
        });
    }

    public void requestCodeSuccess() {
        getCode.setEnabled(false);
        disposableSubscriber = new DisposableSubscriber<Long>() {
            @Override
            public void onNext(Long aLong) {
                int time = (int) (60 - aLong);
                if (getCode != null) {
                    getCode.setText(time + "s后重试");
                }
            }

            @Override
            public void onError(Throwable t) {
                System.out.println(t.toString());
            }

            @Override
            public void onComplete() {
                if (getCode != null) {
                    getCode.setText("再次获取验证码");
                    getCode.setEnabled(true);
                }
                if (!disposableSubscriber.isDisposed()) {
                    disposableSubscriber.dispose();
                    disposableSubscriber = null;
                }
            }
        };

        Flowable.interval(0, 1, TimeUnit.SECONDS, Schedulers.newThread())
                .take(60)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(disposableSubscriber);
    }

    public void setTpwdOK() {
        userData.setPhoneixPwd("1");
        userViewModel.update(userData);
        finish();
    }
}
